package com.dzg.demo.example.aqs;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.FutureTask;
@Slf4j
public class FutureTaskExample {
    public static void main(String[] args) throws Exception {
        CountDownLatch countDownLatch = new CountDownLatch(1);
        FutureTask<String> futureTask = new FutureTask<>(new Callable<String>() {
            @Override
            public String call() throws Exception {
                log.info("do something in callable");
                try {
                    Thread.sleep(1000);
                }finally {
                    log.info("do something in countdown");
                    countDownLatch.countDown();
                }

                return "done";
            }
        });
        new Thread(futureTask).start();
//        new Thread(futureTask).start();
//        Thread.sleep(1000);

        countDownLatch.await();
        log.info("do something in main");

        String result = futureTask.get();
        log.info("result:{}",result);

    }
}
